package springapp.web;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import springapp.entity.CompanyEntity;
import springapp.entity.DesignationEntity;
import springapp.entity.SalaryEntity;
import springapp.service.CompanyService;
import springapp.service.IDesignationService;
import springapp.service.ILocationService;
import springapp.service.SalaryService;
import springapp.util.Utils;

@Controller
public class AddSalaryController implements IControllerConstants {

	@Autowired
	private CompanyService companyService;

	@Autowired
	private IDesignationService designationService;

	@Autowired
	private ILocationService locationService;

	@Autowired
	private SalaryService salaryService;

	@RequestMapping(value = "/addSalary.htm", method = RequestMethod.GET)
	public ModelAndView handleRequest(@RequestParam("salary") String salary,
			@RequestParam("companyId") String companyId,
			@RequestParam("gender") String gender,
			@RequestParam("exp") String experience,
			@RequestParam("desig") String designation,
			@RequestParam("jobType") String jobType,
			@RequestParam("location") String location) throws Exception {
		DesignationEntity entity = designationService
				.getDesignationEntity(designation);
		Integer locationId = locationService.getLocationId(location);

		int companyId2 = Utils.getInt(companyId);
		SalaryEntity salaryEntity = salaryService.addSalary(gender,
				(float) Utils.getFloat(salary), locationId, entity.getId(),
				Utils.getInt(experience), companyId2, jobType);

		CompanyEntity companyEntity = companyService.addSalary(companyId2,
				salaryEntity, entity);
		companyService.setIndustryName(companyEntity);
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("company", companyEntity);
		return new ModelAndView("Company.jsp", "jsonMap", jsonMap);
	}

}
